<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title></title>
	<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
	<link rel="stylesheet" type="text/css" href="../../css/aui-pull-refresh.css" />
	<link rel="stylesheet" type="text/css" href="../../css/font-awesome.css" />
	<link rel="stylesheet" type="text/css" href="../../css/mui.picker.css" />
	<link rel="stylesheet" type="text/css" href="../../css/inspection_list.css" />

</head>

<body>
	<section class="aui-refresh-content" id="pContainer1">
		<!--<button onclick="getgps()"> ceshi</button> -->
		<div class="aui-searchbar" id="search">
			<div class="aui-searchbar-input aui-border-radius aui-padded-r-5" tapmode>
				<i class="aui-iconfont aui-icon-search"></i>
				<form action="javascript:doSearch();">
					<input type="search" placeholder="请输入老人姓名" id="search-input">
				</form>
			</div>
			<div class="aui-searchbar-btn" tapmode>
				取消
			</div>
		</div>
		<div class="aui-content">
			<ul class="aui-list aui-media-list" id="list">

			</ul>
		</div>
	</section>
</body>
<script id="listTemplate" type="text/x-dot-template">
	{{for(var i=0;i
	<it.autOlderList.length;i++){}} <li class="aui-list-item" {{? it.type==0}}onclick="iteminfo('{{=it.autOlderList[i].ocpyOrderId}}')" {{?}}>
		<div class="aui-media-list-item-inner">
			<div class="aui-list-item-media" style="display:none;">
				<!--<img src="{{=it.autOlderList[i].avatar}}">-->
			</div>
			<div class="aui-list-item-inner">
				<div class="aui-list-item-text">
					<div class="aui-list-item-title" style="font-size: 1rem; font-weight:bold;">
						{{=it.autOlderList[i].olderName}}
					</div>
					<div class="aui-list-item-right">
						{{? it.type==0}}
						<div class="aui-label aui-label-danger">未巡检</div>
						{{??}}
						<div class="aui-label aui-label-success">{{=it.autOlderList[i].autDate}}</div>
						{{?}}
					</div>
				</div>
				<div class="aui-list-item-text">
					{{=it.autOlderList[i].buildingName}}<i class="fa fa-angle-right" aria-hidden="true"></i>{{=it.autOlderList[i].floorName}}<i class="fa fa-angle-right" aria-hidden="true"></i>{{=it.autOlderList[i].roomCode}}房间<i class="fa fa-angle-right" aria-hidden="true"></i>{{=it.autOlderList[i].bedCode}}床位
				</div>
			</div>
		</div>
		</li>
		{{}}}
</script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/doT.js"></script>
<script type="text/javascript" src="../../script/util.js"></script>
<script type="text/javascript" src="../../script/aui-pull-refresh.js"></script>
<script type="text/javascript" src="../../script/mui.min.js"></script>
<script type="text/javascript" src="../../script/mui.picker.min.js"></script>

<script type="text/javascript">
	var sourceType = "camera";
	var td;
	var search = {
		pageNo: 1,
		pageSize: 20,
		olderName: '',
		buildingId: '',
		floorId: '',
		roomId: '',
		token: '',
		autFlag: ''
	};

	apiready = function() {
		api.parseTapmode();
		td = api.require('talkingData');
		search.token = $api.getStorage('userinfo').token;
		console.log("userinfo：" + JSON.stringify($api.getStorage('userinfo')));
		search.autFlag = api.pageParam.type;
		sourceType = api.deviceModel=="PDA401"?"camera":"album";
		loadData(search);
	};

	// 筛选
	function pickerData() {
		var city_picker = new mui.PopPicker({
			layer: 3
		});
		api.ajax({
			url: base + 'gero/open/geroOpen/getGeroLayoutInfo',
			method: 'post',
			data: {
				values: {
					geroId: $api.getStorage('userinfo').user.geroId
				}
			}
		}, function(ret, err) {
			if (ret && ret.code == "200") {
				city_picker.setData(ret.result);
			}
		});

		city_picker.show(function(items) {
			search.buildingId = (items[0] || {}).value;
			search.floorId = (items[1] || {}).value;
			search.roomId = (items[2] || {}).value;
			tips('正在进行筛选操作，取消筛选请点左上角按钮。');
			console.log((items[0] || {}).value + "," + (items[1] || {}).value + "," + (items[2] || {}).value);
			loadData(search, "picker");

		});
	}

	function loadData(searchParm) {
		console.log("发送数据：" + JSON.stringify(searchParm));
		// pContainer1.style.marginTop = $api.byId('ast').clientHeight + 'px';
		api.showProgress({});
		if (arguments[1]) {
			$api.html($api.byId('list'), '');
		}
		api.ajax({
			url: base + 'gero/api/autRecord/getOlderList',
			method: 'post',
			data: {
				values: JSON.parse(JSON.stringify(searchParm))
					// values: {
					// 	pageNo: pageNumber,
					// 	pageSize: '20',
					// 	token: $api.getStorage('userinfo').token,
					// 	autFlag: type,
					// 	olderName: searchText
					// }
			}
		}, function(ret, err) {
			api.hideProgress();
			console.log("结果：" + JSON.stringify(ret));
			if (ret && ret.code == "200") {
				ret.type = searchParm.autFlag;
				var template = $api.byId('listTemplate');
				var list_content = $api.byId('list');
				var pagefn = doT.template(template.text);
				//list_content.insertAdjacentHTML('beforeEnd', pagefn(ret));
				//$api.html(list_content, pagefn(ret));
				list_content.insertAdjacentHTML('afterbegin', pagefn(ret));
				pullRefresh.cancelLoading();
			} else {
				doLogin();
			}
		});
	}

	var pullRefresh = new auiPullToRefresh({
		container: document.querySelector('.aui-refresh-content'),
		triggerDistance: 100
	}, function(ret) {
		if (ret.status == "success") {
			var searchValue = document.getElementById("search-input").value;
			console.log(searchValue);
			if (searchValue) {
				$api.html($api.byId('list'), '');
				search.olderName = searchValue;
				search.pageNo = 1;
				loadData(search);
			} else {
				search.pageNo += 1;
				loadData(search);
			}

		}
	});

	function iteminfo(id) {
		// 判断失败次数
		var err = $api.getStorage(id);
		console.log("失败次数："+err);
		if(err >= 2){
			api.alert({
			    title: '温馨提示',
			    msg: '巡检失败次数过多，请联系12349话务人员进行处理。',
			}, function(ret, err){

			});
		}

		if (api.systemType==="ios" || api.deviceId=="359261051214885" || api.deviceModel!="PDA401") {
			api.actionSheet({
			    title: '巡检方式',
			    cancelTitle: '取消',
			    buttons: ['现场拍照','相册照片']
			}, function(ret, err){
			    if(ret){
						switch (ret.buttonIndex) {
							case 1:
								uploadPic('camera',id);
								break;
							case 2:
								uploadPic('album',id);
								break;
						}
			    }
			});
		}else {
			uploadPic('camera',id);
		}

	}

	function uploadPic(sourceType,id){
		api.getPicture({
			sourceType: sourceType,
			encodingType: 'jpg',
			mediaValue: 'pic',
			destinationType: 'url',
			targetWidth: 480,
			targetHeight: 800,
			quality: 100,
			saveToPhotoAlbum: false
		}, function(ret, err) {
			console.log(ret);
			if (ret && ret.data != "") {
				//var b2 = ret.base64Data.substring(ret.base64Data.indexOf(",") + 1);
				//console.log("原图："+b2);
				var b2 = ret.data;
				if (api.systemType === "ios") {
					//bd_location(id, b2);
					ios_location(id, b2);
				} else if (api.systemType === "android") {
					bd_location(id, b2);
				}
			} else {
				api.alert({
					msg: '请拍照后再进行巡检。'
				}, function(ret, err) {
					//coding...
				});
			}
		});
	}

	function doSearch() {
		var searchValue = document.getElementById("search-input").value;
		if (searchValue) {
			$api.html($api.byId('list'), '');
			search.olderName = searchValue;
			search.pageNo = 1;
			loadData(search);
		} else {
			$api.html($api.byId('list'), '');
			search.olderName = '';
			search.pageNo = 1;
			loadData(search);
		}
	}

	// 重新加载数据
	function reloadData() {
		$api.html($api.byId('list'), '');
		search.pageNo = 1;
		search.olderName = '';
		search.buildingId = '';
		search.floorId = '';
		search.roomId = '';
		closeTips();
		loadData(search);
	}

	// 打开图片窗口
	function openWinPhoto(id,photo,lon,lat){
		console.log("id:"+id);
		api.openWin({
		    name: 'browse_inspection_photo',
		    url: api.wgtRootDir + '/html/inspection/photo.html',
		    pageParam: {
		        id: id,
						photo: photo,
						lon:lon,
						lat:lat
		    }
		});
	}

	// 苹果定位
	function ios_location(id, photo) {
		//获取位置信息成功
		//openBrowse(api.wgtRootDir + '/html/inspection/photo.html', '日常巡检', false, 'photo_show', photo);
		//saveFaceVerificationInfo(id, photo, '122.212677', '40.640846');
		openWinPhoto(id,photo,'122.212677','40.640846');
	}

	// 百度定位
	function bd_location(id, photo) {
		var bmLocation = api.require('bmLocation');
		bmLocation.configManager({
			accuracy: 'hight_accuracy',
			//filter: 1.0,//位置更新最小距离，默认就是1.0，安卓不支持
			activityType: 'automotiveNavigation',
			coordinateType: 'GCJ02',
			locationTimeout: 10,
			reGeocodeTimeout: 10
		});
		bmLocation.singleLocation({
			reGeocode: false,
			netWorkState: false
		}, function(ret) {
			var sta = ret.status;
			console.log("定位状态：" + sta);
			if (sta) {
				var lat = ret.location.latitude;
				var lon = ret.location.longitude;
				var t = ret.timestamp;
				var str = '经度：' + lon + '<br>';
				str += '纬度：' + lat + '<br>';
				str += '更新时间：' + t + '<br>';
				//api.alert({ msg: str });
				console.log(str);
				// 保存
				//saveFaceVerificationInfo(id, photo, lon, lat);
				openWinPhoto(id,photo,lon,lat);
			} else {
				api.alert({
					msg: '定位发生错误，请打开定位。'
				});
			}
		});
	}

	function getgps() {
		var bmLocation = api.require('bmLocation');
		bmLocation.configManager({
			accuracy: 'hight_accuracy',
			//filter: 1.0,//位置更新最小距离，默认就是1.0，安卓不支持
			activityType: 'automotiveNavigation',
			coordinateType: 'GCJ02',
			locationTimeout: 10,
			reGeocodeTimeout: 10
		});
		bmLocation.singleLocation({
			reGeocode: false,
			netWorkState: false
		}, function(ret) {
			console.log(JSON.stringify(ret));
			var sta = ret.status;
			if (sta) {
				var lat = ret.location.latitude;
				var lon = ret.location.longitude;
				var t = ret.timestamp;
				var str = '经度：' + lon + '<br>';
				str += '纬度：' + lat + '<br>';
				str += '更新时间：' + t + '<br>';
				api.alert({
					msg: str
				});
			} else {
				api.alert({
					msg: '发生错误'
				});
			}
		});
	}
</script>

</html>
